package com.itheima.publisher;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TopicDemo {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendTopicMessages() {
        System.out.println("=== 开始发送Topic消息 ===");
        
        // 演示不同的routing key模式
        
        // 1. 发送到 topic.queue1 (匹配 order.*)
        System.out.println("发送消息到 topic.queue1 (匹配 order.*)");
        rabbitTemplate.convertAndSend("hmall.topic", "order.create", "订单创建消息");
        rabbitTemplate.convertAndSend("hmall.topic", "order.update", "订单更新消息");
        rabbitTemplate.convertAndSend("hmall.topic", "order.delete", "订单删除消息");
        
        // 2. 发送到 topic.queue2 (匹配 *.create)
        System.out.println("发送消息到 topic.queue2 (匹配 *.create)");
        rabbitTemplate.convertAndSend("hmall.topic", "user.create", "用户创建消息");
        rabbitTemplate.convertAndSend("hmall.topic", "product.create", "商品创建消息");
        rabbitTemplate.convertAndSend("hmall.topic", "order.create", "订单创建消息（同时匹配queue1和queue2）");
        
        // 3. 发送到 topic.queue3 (匹配 order.#)
        System.out.println("发送消息到 topic.queue3 (匹配 order.#)");
        rabbitTemplate.convertAndSend("hmall.topic", "order.create", "订单创建消息（同时匹配queue1、queue2和queue3）");
        rabbitTemplate.convertAndSend("hmall.topic", "order.update.status", "订单状态更新消息");
        rabbitTemplate.convertAndSend("hmall.topic", "order.payment.success", "订单支付成功消息");
        rabbitTemplate.convertAndSend("hmall.topic", "order.shipping.tracking", "订单物流跟踪消息");
        
        // 4. 发送不匹配任何队列的消息
        System.out.println("发送不匹配任何队列的消息");
        rabbitTemplate.convertAndSend("hmall.topic", "user.update", "用户更新消息（不匹配任何队列）");
        rabbitTemplate.convertAndSend("hmall.topic", "product.delete", "商品删除消息（不匹配任何队列）");
        
        System.out.println("=== Topic消息发送完成 ===");
    }
} 